package com.example.gate.grpc;

import com.example.common.constants.ServiceNames;
import com.example.common.grpc.GrpcClientBase;
import com.example.common.grpc.GrpcClientManager;
import com.example.common.grpc.ServerIdLoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * Gate服务的gRPC客户端
 * 使用负载均衡器管理多个服务实例的连接
 */
@Slf4j
@Service
public class SeverGrpcClient extends GrpcClientBase {


    public SeverGrpcClient(GrpcClientManager clientManager) {
        super(clientManager);
    }

    @Scheduled(fixedRate = 10000)
    private void heartbeat() {
        try {
            int currentServerId = clientManager.getCurrentServerId();
            log.debug("开始心跳检测，当前服务器ID: {}", currentServerId);

            // 向Lobby服务发送心跳
          /*  new Thread(() -> {

            }).start();*/
            sendHeartbeatToService(ServiceNames.LOBBY);
            // 向Game服务发送心跳
           /* new Thread(() -> {

            }).start();*/
            sendHeartbeatToService(ServiceNames.GAME);
        } catch (Exception e) {
            log.error("心跳检测失败", e);
        }
    }


    /**
     * 获取当前服务器ID
     */
    public int getCurrentServerId() {
        return clientManager.getCurrentServerId();
    }
}